<?php

/*
 * Action -4$
 *
 * You may trash a Treasure from your hand. Each other player with 5 or more cards in hand
 *  discards a copy of it (or reveals a hand without it). Gain a Treasure card costing up 
 * to $3 more than the trashed card, putting it on top of your deck.
 * 
 * 
 */

class Taxman extends Card implements iAction, iKingdom, iGuilds, iAttack {

    public function __construct() {
        parent::__construct("Taxman", 4);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDisplayName($quantity = 1) {
        if ($quantity == 1) {
            return "Taxman";
        }
        return $quantity . " Taxmen";
    }

    public function getDescription() {
        return array(
            "You may trash a Treasure from your hand. Each other player with 5 or more cards in hand discards a copy of it (or reveals a hand without it). Gain a Treasure card costing up to $3 more than the trashed card, putting it on top of your deck."
        );
    }

    public function play(Game $game, $isRealCard) {
        $game->attack($this);

        $playHand = $game->getPlayHand();
        $treasures = array();
        foreach ($playHand as $card) {
            if ($card instanceof iTreasure) {
                $treasures[] = $card;
            }
        }

        if (sizeof($treasures) > 0) {
            $treasures = getUniqueCards($treasures);
            $treasures[] = new Option(1, 'None');

            $output = MySocket::GetUserReply($game, Input_ChooseOne, $treasures, 1, 1, null, "Choose card to trash");
            $answer = $output[0];
            if ($answer != 'None') {
                $treasure_card = $game->moveCardFromPileToPile($answer, PlayHand, Trash);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($treasure_card), " trashes " . ScreenDisplay::$LMSG);

				$price = $treasure_card->getPrice($game);
				$optional_treasures = $game->getBoardTreasureCardsByPrice(new Price(), $price->add(3));
				$output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_treasures, 1, 1, null, "Choose a card to gain");

				$new_card_name = $output[0];
				$new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DrawPile);
				ScreenDisplay::addScreenMessage($game, false, ", gain " . nicePrint($new_card) . " and put in on his deck", ", gains " .ScreenDisplay::$LMSG ." and puts it on his deck");
			
                $names = $game->getOtherPlayersNames(true);
                foreach ($names as $name) {
                    $playHandOther = $game->getPlayHand($name);
                    $playHandSize = sizeof($playHandOther);
                    if ($playHandSize >= 5) {

                        $playHandOther = $game->getPlayHand($name);

                        $treasure_card_found = false;
                        foreach ($playHandOther as $card) {
                            if ($card->getKey() == $treasure_card->getKey()) {
                                $treasure_card_found = true;
                                break;
                            }
                        }
                        if ($treasure_card_found) {
                            $card = $game->moveCardFromPileToPile($treasure_card, PlayHand, DiscardPile, $name);
                            ScreenDisplay::addScreenMessage($game, true, "discard  " . nicePrint($card), "discards " . ScreenDisplay::$LMSG , $name);
                        } else {
                            ScreenDisplay::addScreenMessage($game, true, "reveal  " . nicePrint($playHandOther), "reveals " . ScreenDisplay::$LMSG, $name);
                        }
                    }
                }
            }
        }
    }

}

?>